/**
 * Created by Administrator on 2017/10/19.
 */
var mongoose = require('mongoose');
const  Comment=require('../models/comment.model');

exports.create=function (req,res,next) {
	const comment=new Comment(req.body);
	comment.save()
		.then(data=>{
			res.json(data);
		})
}
exports.get=function (req,res,next) {
	var id=req.params.id;
	Comment.findById(id,function (err,data) {
		res.json(data);
	})
}
exports.updata=function (req,res,next) {
	const comment=new  Comment(req.body);
	var id=req.params.id;
	Comment.findByIdAndUpdate(id,{$set:req.body},{new:false})
		.then(date=>{
			res.json(date);
		})
}

exports.remove=function (req,res,next) {
	var id=req.params.id;
	Comment.findByIdAndRemove(id,function (err,data) {
		res.json(data);
	})
		.then(date=>{
			res.json(date);
		})
}

exports.list=function (req,res,next) {
	var page = (req.body.page)?req.body.page:1;
	var rows =(req.body.limit)?req.body.limit:8;
	var queryCondition={};
	if (req.body.newId&&req.body.newId.trim().length>0){
		newId=req.body.newId;
		queryCondition={'newId':new RegExp(newId,'i')}
	}
	
	Comment.paginate(queryCondition,{sort:{_id:-1},page:parseInt(page),limit:parseInt(rows)},function (err,result) {
		result.rows=result.docs;
		delete result.docs;
		res.json(result);
	});
	
}

exports.deletes=function (req,res,next) {
	var ids =req.body.ids;
	if (ids.length>0){
		console.log(req.body);
		Comment.remove({ _id:{$in:ids.split(',')}}).then(data=>{
			res.json({"msg":'删除成功！','status':200});
		})
	}
	else{
		res.json({"msg":'删除失败！','status':404})
	}
}